package com.flw.eslab.service.impl;

import java.io.File;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.flw.eslab.dao.GroupDao;
import com.flw.eslab.dao.UserDao;
import com.flw.eslab.dto.PageInfo_Dto;
import com.flw.eslab.model.Group;
import com.flw.eslab.model.Task;
import com.flw.eslab.service.GroupManager;

@Service("groupManager")
public class GroupManagerImpl implements GroupManager {
	@Resource
	private GroupDao groupDao;
	@Resource
	private UserDao userDao;
	
	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public GroupDao getGroupDao() {
		return groupDao;
	}

	public void setGroupDao(GroupDao groupDao) {
		this.groupDao = groupDao;
	}

	@Override
	public boolean add(Group group) {
		if(group.getName()!=null){
			if(groupDao.findByHql("from Group where name="+group.getName())==null){
				groupDao.save(group);
				return true;
			}
		}
		return false;
	}

	@Override
	public boolean delete(Group group) {
		if(group.getId()==null) return false;		
		if(userDao.findByGroup(group)==null){
			groupDao.deleteById(Group.class, group.getId());
			return true;
		}
		return false;
	}

	@Override
	public boolean modify(Group group) {
		if(group.getId()==null) return false;
		if(groupDao.findById(Group.class, group.getId())!=null){
			if(group.getName()!=null){
				if(groupDao.findByHql("from Group where name="+group.getName())==null){
					groupDao.update(group);
					return true;
				}
			}
		}
		return false;
	}

	@Override
	public File output() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public PageInfo_Dto load(PageInfo_Dto pageInfo_Dto) {
		int pageNow=pageInfo_Dto.getPageNow()==null?1:pageInfo_Dto.getPageNow();
		int pageSize=pageInfo_Dto.getPagesize()==null?5:pageInfo_Dto.getPagesize();	
		return groupDao.findByPageInfo(Group.class, pageNow, pageSize, null, null);
	}

}
